<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use Illuminate\Http\Request;
use App\Models\Node;
class RoleController extends BaseController
{
    /**
     *列表
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //获取搜索框
        $kw = $request->get('kw','');

        //分页 搜索
        //参数1,  变量值存 则执行  参数2--> 匿名函数
        $data =  Role::when($kw,function ($query) use ($kw) {
            $query->where('name','like',"%{$kw}%");
        })->paginate($this->pagesize);

        return view('admin.role.index',compact('data','kw'));
    }

    /**
     * 添加显示
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * 添加处理
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try{
            $this->validate($request,[
                'name'=>'required|unique:roles,name'
            ]);
        }catch(\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }
        //接收all 所有数据
        //入库操作
        Role::create($request->only('name'));

        return ['status'=>0,'msg'=>'添加角色成功'];
    }

    /**
     * 根据ID显示对应信息
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * 修改显示
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(int $id)
    {
        $model = Role::find($id);
        return view('admin.role.edit',compact('model'));
    }

    /**
     * 修改处理
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //异常处理
        try{
            $this->validate($request,[
                'name'=>'required|unique:roles,name,' . $id . ',id'   //排除id=3的那行为name的字段值
            ]);
        }catch(\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }

        //修改入库
        Role::where('id',$id)->update($request->only(['name']));
        return ['status'=>0,'msg'=>'修改角色成功'];

    }


    //给角色分配权限
    public function node(Role $role){
        //dump($role->nodes->toArray());
        //dump($role->nodes()->pluck('name'));
        //读出所有权限
        $nodeAll = (new Node())->getAllList();
        //读取角色所拥有的权限
        $nodes = $role->nodes()->pluck('id')->toArray();
        return view('admin.role.node',compact('role','nodeAll','nodes'));
    }

    //分配权限处理
    public function nodeSave(Request $request,Role $role){
        //关联模型的数据同步
        $role->nodes()->sync($request->get('node'));
        return redirect(route('admin.role.index',$role));
    }




    /**
     * 删除操作
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
